/***************************************************************************
 *   Copyright (C) 2008 by Eduardo Gurgel Pinho                            *
 *   edgurgel@gmail.com                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/

#include "disc.h"

#include "math/ray.h"
#include "material/object_material.h"

#include <limits>
#include <cmath>

namespace Sirat {

namespace Object {

Disc::Disc(Material::Object_Material* _object_material,const Math::Vector& _center = Math::Vector(0.0,0.0,0.0),const Math::Vector& _normal = Math::Vector(1.0,1.0,1.0),const double _radius = 0.5) : Object(_object_material), center(_center) , normal(_normal.normalize()) , radius(_radius) {

}

Disc::Disc() : Object() { }

Disc::Disc(const Disc& _disc) : Object(_disc) {
	center = _disc.center;
	normal = _disc.normal;
	radius = _disc.radius;
}

Disc::~Disc() { }

Math::Vector Disc::get_center() const { return center; }

Math::Vector Disc::get_normal(const Math::Vector& _point) const { return normal.normalize(); }

double Disc::get_radius() const { return radius; }

void Disc::set_center(const Math::Vector& _center) { center = _center; }

void Disc::set_normal(const Math::Vector& _normal) { normal = _normal; }

void Disc::set_radius(const double _radius) { radius = _radius; }

tuple<double,Math::Vector,Object*> Disc::intersection(const Math::Ray& _ray) {
	// This intersection is similar to Plane::intersection + distance <= radius
	std::numeric_limits<double> double_limit;
	const double infinity = double_limit.infinity();
	
	const double n_dot_d = normal.dot_product(_ray.get_direction());
	Math::Vector intersection_point;
	
	if(n_dot_d < 0.0) {
		const double distance = (normal.dot_product(center.sub_vect(_ray.get_origin()))) / (n_dot_d);
		
		if(distance >= 0.0) { 
			intersection_point = _ray.get_origin() + _ray.get_direction()*distance;
			
			if(intersection_point.distance(center) <= radius)
			return make_tuple(distance,intersection_point,this);
			else return make_tuple(infinity,intersection_point,this);
		}
	}
	return make_tuple(infinity,intersection_point,this);
}

Disc& Disc::operator=(const Disc& _disc) {
	Object::operator=(_disc);
	center = _disc.center;
	normal = _disc.normal;
	radius = _disc.radius;
	return *this;
}

}

}
